package com.fzy.common.bean;

import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class ReflectionUtil {

    private ReflectionUtil() {
    }


    public static Object invokeMethod(Object methodObject, String methodName,
                                      Object... args) throws Exception {
        Class ownerClass = methodObject.getClass();
        Class[] argsClass = new Class[args.length];
        for (int i = 0, j = args.length; i < j; i++) {
            argsClass[i] = args[i].getClass();
        }
        Method method = ownerClass.getMethod(methodName, argsClass);
        return method.invoke(methodObject, args);
    }


    public static Object invokeStaticMethod(String className,
                                            String methodName, Object... args) throws Exception {
        Class ownerClass = Class.forName(className);

        Class[] argsClass = new Class[args.length];

        for (int i = 0, j = args.length; i < j; i++) {
            argsClass[i] = args[i].getClass();
        }

        Method method = ownerClass.getMethod(methodName, argsClass);

        return method.invoke(null, args);
    }


    public static Object newInstance(String className, Object... args)
            throws Exception {
        Class newoneClass = Class.forName(className);

        Class[] argsClass = new Class[args.length];

        for (int i = 0, j = args.length; i < j; i++) {
            argsClass[i] = args[i].getClass();
        }

        Constructor cons = newoneClass.getConstructor(argsClass);

        return cons.newInstance(args);

    }


    public boolean isInstance(Object obj, Class cls) {
        return cls.isInstance(obj);
    }


    public static Object getByArray(Object array, int index) {
        return Array.get(array, index);
    }

    public static Object getProperty(Object owner, String fieldName)
            throws Exception {
        Class<?> ownerClass = owner.getClass();

        Field field = ownerClass.getField(fieldName);


        //Field field = ownerClass.getDeclaredField(fieldName);
        //field.setAccessible(true); 

        Object property = field.get(owner);

        return property;
    }

    public static Object getStaticProperty(String className, String fieldName)
            throws Exception {
        Class<?> ownerClass = Class.forName(className);

        Field field = ownerClass.getField(fieldName);

        Object property = field.get(ownerClass);

        return property;
    }

    public static void main(String[] args) throws Exception {
        // System.out.println(getProperty(new MainTest(),"name"));
        // System.out.println(getStaticProperty("main.MainTest","name"));
        /*String[] arrs = { "fzy" }; invokeMethod(new MainTest(), "getPrint",
         * arrs);*/
        /*MainTest mt = (MainTest) newInstance("main.MainTest");
        mt.getPrint();*/

        String[] strr = {"10", "9", "8"};
        String sss = strr[0];
        System.out.println(getByArray(strr, 0));
    }
}
